//
//  BCWalletExchangeFromToController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/22.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCWalletExchangeFromToController.h"

#import "BCWalletExchangeDataModel.h"
//
#import "BCWalletExchangeFromToPickerView.h"


static CGFloat BCWalletExchangeFromToControllerTopHeight = 40.0f;
static CGFloat BCWalletExchangeFromToControllerPickHeight = 200.0f;

@interface BCWalletExchangeFromToController ()

@property (nonatomic, strong) UIView *pickContentView;

/**
 *pickView
 */
@property (nonatomic, strong) BCWalletExchangeFromToPickerView *pickerView;

/**
 *cancelButton
 */
@property (nonatomic, strong) QMUIButton *cancelButton;

/**
 *doneButton
 */
@property (nonatomic, strong) QMUIButton *doneButton;


#pragma mark - data

/**
 *BCWalletExchangeDataModel
 */
@property (nonatomic, weak) BCWalletExchangeDataModel *dataModel;

/**
 *BCWalletExchangeFromToChooseComplete
 */
@property (nonatomic, copy) BCWalletExchangeFromToChooseComplete chooseComplete;

@end

@implementation BCWalletExchangeFromToController

+ (void)showFromController:(UIViewController *)fromController data:(BCWalletExchangeDataModel *)dataModel complete:(BCWalletExchangeFromToChooseComplete)complete{
    BControllerWindow *window = [[BControllerWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    BCWalletExchangeFromToController *vc = [BCWalletExchangeFromToController new];
    vc.fromController = fromController;
    vc.showWindow = window;
    vc.dataModel = dataModel;
    vc.chooseComplete = complete;
    window.rootViewController = vc;
    [window makeKeyAndVisible];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    //
    [self showContentAnimation];
}

/**
 content高度
 */
- (CGFloat)contentHeight {
    return BCWalletExchangeFromToControllerTopHeight+BCWalletExchangeFromToControllerPickHeight+Height_Bottom_SafeArea;
}
- (void)setupSubviews {
    [self.view addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    //
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo([self contentHeight]);
        make.bottom.mas_equalTo(self.view).offset([self contentHeight]);
    }];
    //
    [self setupContents];
    //
    [self.view layoutIfNeeded];
}
- (void)setupContents {
    self.cancelButton = [QMUIButton new];
    [self.cancelButton setTitle:BCLocalizedKey(@"Cancel") forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor qmui_colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.contentView);
        make.height.mas_equalTo(BCWalletExchangeFromToControllerTopHeight);
    }];
    //
    self.doneButton = [QMUIButton new];
    [self.doneButton setTitle:BCLocalizedKey(@"Done") forState:UIControlStateNormal];
    [self.doneButton setTitleColor:[UIColor qmui_colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.cancelButton);
    }];
    //
    self.cancelButton.titleLabel.font = self.doneButton.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    self.cancelButton.contentEdgeInsets = self.doneButton.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 12);
    //action
    [self.cancelButton addTarget:self action:@selector(cancelButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    [self.doneButton addTarget:self action:@selector(doneButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.pickContentView = [UIView new];
    [self.contentView addSubview:self.pickContentView];
    [self.pickContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCWalletExchangeFromToControllerTopHeight);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(BCWalletExchangeFromToControllerPickHeight);
    }];
    //
    self.pickerView = [BCWalletExchangeFromToPickerView new];
    [self.pickContentView addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.pickContentView);
    }];
    self.pickerView.pickSource = self.dataModel;
    //
    WEAK_SELF
    self.pickerView.chooseSame = ^(BOOL theSame) {
        wself.doneButton.alpha = theSame?0:1;
    };
}
//
- (void)showContentAnimation {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(0);
    }];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)dismissContentDone:(BOOL)done {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset([self contentHeight]);
    }];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (done) {
            UserWalletExchangeDataCurrencyModel *from = self.pickerView.from;
            UserWalletExchangeDataCurrencyModel *to = self.pickerView.to;
            if (self.chooseComplete) {
                self.chooseComplete(from, to);
            }
        }
        [self dismissWindow];
    }];
}

#pragma mark - cancelButtonDidClickedAction
- (void)cancelButtonDidClickedAction {
    [self dismissContentDone:false];
}
- (void)doneButtonDidClickedAction {
    UserWalletExchangeDataCurrencyModel *from = self.pickerView.from;
    UserWalletExchangeDataCurrencyModel *to = self.pickerView.to;
    if (from&&to) {
        [self dismissContentDone:true];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self.view];
    if (!CGRectContainsPoint(self.contentView.frame, touchPoint)) {
        [self dismissContentDone:false];
    }
}

@end
